---
title: "Spring中有用的类"
date: 2021-1-13
categories:
- java
- spring
tags:
---

<div id="content">
<p>
org.springframework.core.ResolvableType 能用于解析泛型类
</p>
<div class="org-src-container">
<pre class="src src-java"><span style="font-weight: bold;">private</span> <span style="font-weight: bold; text-decoration: underline;">HashMap</span>&lt;<span style="font-weight: bold; text-decoration: underline;">Integer</span>, <span style="font-weight: bold; text-decoration: underline;">List</span>&lt;<span style="font-weight: bold; text-decoration: underline;">String</span>&gt;&gt; <span style="font-weight: bold; font-style: italic;">myMap</span>;

   <span style="font-weight: bold;">public</span> <span style="font-weight: bold; text-decoration: underline;">void</span> <span style="font-weight: bold;">example</span>() {
       <span style="font-weight: bold; text-decoration: underline;">ResolvableType</span> <span style="font-weight: bold; font-style: italic;">t</span> = ResolvableType.forField(getClass().getDeclaredField(<span style="font-style: italic;">"myMap"</span>));
       t.getSuperType(); <span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">AbstractMap&lt;Integer, List&lt;String&gt;&gt;</span>
       t.asMap(); <span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">Map&lt;Integer, List&lt;String&gt;&gt;</span>
       t.getGeneric(0).resolve(); <span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">Integer</span>
       t.getGeneric(1).resolve(); <span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">List</span>
       t.getGeneric(1); <span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">List&lt;String&gt;</span>
       t.resolveGeneric(1, 0); <span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">String</span>
   }
</pre>
</div>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2021-1-13</p>
<p class="author">Author: amo</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
